package org.example.controller;

import jakarta.annotation.Resource;
import org.example.bean.Book;
import org.example.service.BookService;
import org.example.util.JsonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Auther:Liu
 * @Testname:BookController
 * @Date:2025/8/6 11:43
 */
@RestController
@RequestMapping("/Book")
public class BookController {

    @Resource
    private BookService bookService;

    @GetMapping("/findAll/{page}/{size}")
    public JsonResult findAll(@PathVariable("page") int page,@PathVariable("size")int size){
        return bookService.find(page,size);
    }

    @PostMapping("/save")
    public JsonResult BookSave(Book book, @RequestParam("file") MultipartFile file) throws IOException {
        return bookService.saveBook(book,file);
    }

    @GetMapping("/findBook/{id}")
    public JsonResult findBookById(@PathVariable("id")String id){
        return bookService.findBookById(id);
    }

    @PutMapping("/update")
    public JsonResult updateBook(@RequestBody Book book){
        return bookService.UpdateBook(book);
    }

    @DeleteMapping("/{id}")
    public JsonResult deleteBookById(@PathVariable("id")String id){
        return bookService.deleteBook(id);
    }

    @GetMapping("/category/{id}")
    public JsonResult findBookByCateGoryId(@PathVariable("id") String cateGoryId){
        return bookService.findBookByCateGoryId(cateGoryId);
    }

    @GetMapping("/findcateGoryIdToShow/{cateGoryId}")
    public JsonResult findBookByCateGoryIdToShow(@PathVariable("cateGoryId")String cateGoryId){
        return bookService.findBookByCateGoryIdToShow(cateGoryId);
    }

    @GetMapping("/all")
    JsonResult findAllBook(){
        return bookService.findAllBook();
    }
}
